'''
Created on 24/08/2010

@author: kivson
'''
import os.path
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy.orm import relationship, backref


Base = declarative_base()

tabela_tag_arquivo = Table('tabela_tag_arquivo', Base.metadata,
                           Column('arquivo_id', Integer, ForeignKey('arquivos.id')),
                           Column('tag_id', Integer, ForeignKey('tags.id'))
                           )


class Arquivo(Base):
    __tablename__ = 'arquivos'
    id = Column(Integer, primary_key = True)
    pasta = Column(String)
    md5 = Column(String, unique=True)
    nomeOriginal = Column(String)
    tamanho = Column(Integer)
    tipoCriptArq_id = Column(Integer, ForeignKey('tipoDeCriptografiaDeArquivo.id'))
    tipoCriptArq = relationship('TipoCriptoArq')
    tags = relationship('Tag', secondary=tabela_tag_arquivo)
    
    def __init__(self, nome, hashmd5, pasta = '/'):
        '''
        Constructor
        '''
        self.md5 = hashmd5
        self.nomeOriginal = nome
        self.pasta = pasta
    
    #@property
    def nomeCompleto(self):
        return os.path.join(self.pasta, self.nomeOriginal)
    
    
    def __str__(self):
        return self.nomeOriginal
    
    def __repr__(self):
        return self.nomeOriginal

class Tag(Base):
    __tablename__ = 'tags'
    id = Column(Integer, primary_key = True)
    nomeTag = Column(String, nullable=False, unique=True)
    arquivos = relationship('Arquivo', secondary=tabela_tag_arquivo)
    
    def __init__(self, tag ):
        '''
        Constructor
        '''
        self.nomeTag = tag


class TipoCriptoArq(Base):
    __tablename__ = 'tipoDeCriptografiaDeArquivo'
    id = Column(Integer, primary_key = True)
    nome = Column(String, nullable=False, unique=True)
    desc = Column(TEXT)
    
    def __init__(self, nome, desc):
        '''
        Constructor
        '''
        self.nome=nome
        self.desc = desc
    
